import { getUserInfo, editUserInfo } from '@/static/js/user'
import { imgUpload } from '@/utils/request.js'
import { completeTask } from '@/static/js/points.js';
export default {
	data() {
		return {
			menuList: [
				{
					name: '头像',
					remark: '',
					handler: 'changeAvatar',
					isShow: true
				},
				{
					name: '手机号码',
					remark: '',
					isShow: true
				},
				{
					name: '昵称',
					remark: '',
					isShow: true
				},
				{
					name: '个人介绍',
					remark: '',
					isShow: true
				},
				{
					name: '我的邮箱',
					remark: '',
					isShow: true
				}
			],
			userInfo: {},
			showPointsTips: false,
			pointsObj: {},
		}
	},
	methods: {
		//获取个人信息
		getUserInfo() {
			getUserInfo({}).then((res) => {
				let info = res.data
				this.userInfo = info
				this.filterMenu();
			})
		},
		//菜单数据处理
		filterMenu() {
			const { avatar, mobile, nickname, introduce, email, team_count, bio } = this.userInfo;

			const mobileTxt = `${mobile.slice(0, 3)}****${this.userInfo.mobile.slice(7, 11)}`;

			const params = [avatar, mobileTxt, nickname, introduce, email, team_count];

			this.menuList.forEach((ele, index) => {
				if (index === 3) {
					ele.remark = bio;
				} else {
					ele.remark = params[index];
				}
			});
		},

		//点击事件
		menuClick(item) {
			if (item.handler) {
				this[item.handler]();
			} else if (item.url) {
				this.$tools.href(item.url);
			}
		},

		//显示头像弹窗
		changeAvatar() {
			let that = this;
			uni.showActionSheet({
				itemList: ['查看头像', '更换头像'],
				success: function (res) {
					if (res.tapIndex == 0) {
						uni.previewImage({
							urls: [that.userInfo.avatar],
						})
					} else {
						that.chooseImage();
					}
				},
				fail: function (res) {
					console.log(res.errMsg);
				}
			});
		},

		//从文件夹选择头像图片
		chooseImage() {
			uni.chooseImage({
				count: 1, //默认9
				sizeType: ['original', 'compressed'],
				sourceType: ['album', 'camera'],
				success: (res) => {
					if (res.tempFilePaths.length > 0) {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							this.toUpload(res.tempFilePaths[i]);
						}
					}
				},
				fail: (res) => {
					console.log(res);
				}
			});
		},

		//上传图片
		toUpload(img) {
			imgUpload('/api/common/upload', img).then((res) => {
				if (res.code == 1) {
					//更新
					this.upUserInfo(res.data.url);
				} else {
					this.$tools.showToast(res.msg);
				}
			})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},

		//更新个人信息
		upUserInfo(url) {
			let params = {
				avatar: url,
				rLoading: true,
				email: this.userInfo.email,
			};
			editUserInfo(params).then(res => {
				if (res.code == 1) {

					this.$tools.msg('修改成功');
					this.completeQuest('B1');
					this.getUserInfo();
				} else {
					this.$tools.msg(msg);
				}
			})
		},

		//确定
		confirm() {
			let editUser = {
				nickname: this.menuList[2].remark,
				bio: this.menuList[3].remark,
				email: this.menuList[4].remark,
			}
			if (editUser.nickname == '') {
				this.$tools.msg('请输入昵称')
				return
			}
			if (editUser.nickname == this.userInfo.nickname && editUser.bio == this.userInfo.bio && editUser.email == this.userInfo.email) {
				return false
			}
			editUserInfo(editUser).then(res => {
				if (res.code == 1) {
					this.$tools.msg('修改成功')
					if (editUser.nickname != this.userInfo.nickname) {
						this.completeQuest('B2');
					}
					if (editUser.email != this.userInfo.email) {
						setTimeout(() => {
							this.completeQuest('B3');
						}, 3000);
					}
					setTimeout(() => {
						this.getUserInfo()
					}, 3000)

				} else {
					this.$tools.msg(res.msg)
				}
			})
		},


		//完成任务
		completeQuest(code) {
			completeTask({ code }).then(res => {
				if (res.code == 1) {
					setTimeout(() => {
						this.showPointsTips = true;
					}, 1000);
					this.pointsObj = res.data;
				}
			})
			setTimeout(() => {
				this.showPointsTips = false;
			}, 3000);
		},


	},
	computed: {

	},
	onLoad(options) {
		this.getUserInfo()
	},
	onShow() {
	},
}